﻿//===============================================================================
// Copyright © NextDocs Corporation 2006-2012.  All rights reserved.
//===============================================================================

using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace LifeInSharePoint.Metro.Layouts.LifeInSharePoint.Metro.admin
{
    public partial class Theme : LayoutsPageBase
    {
        private bool StoredGlobalAccordionValue;
        private bool StoredGlobalRibbonValue;
        private string StoredAccordionTypeValue = string.Empty;
        private string StoredQuicklaunchPositionValue = string.Empty;
        private bool StoredAccordionValue;

        protected void Page_Load(object sender, EventArgs e)
        {
            BtnApply.Click += BtnApplyClick;
            GetPropertyBagValues();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            DisableControlChecker();

            //Set Checkbox Value
            if (SPContext.Current.Web.IsRootWeb)
            {
                ApplyAccordionGloballyCB.Checked = StoredGlobalAccordionValue;
            }

            if (!Page.IsPostBack)
            {
                PopulateAccordionTypeDropDown(this.AccordionTypeDropdown, StoredAccordionTypeValue);
                PopulateQuickLaunchPositionDropDown(this.QuickLaunchPosition, StoredQuicklaunchPositionValue);
            }

            //Set Accordion Inheritance Checkbox Value
            AccordionCheckbox.Checked = StoredAccordionValue;
            //Set Floated Ribbon Checkbox Value
            FloatRibbonCB.Checked = StoredGlobalRibbonValue;

            if (StoredGlobalAccordionValue)
                AccordionStatusMessage.InnerHtml = "Accordion Settings are Inherited. Please navigate to Site Collection Root to manage. <br />";
        }

        /// <summary>
        /// On apply button click action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnApplyClick(object sender, EventArgs e)
        {
            // Save Blog Settings
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "MetroAccordionActivated", AccordionCheckbox.Checked.ToString());
            Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Web, "GlobalMetroAccordionType", AccordionTypeDropdown.SelectedValue);
            //Save Global Values
            if (SPContext.Current.Web.IsRootWeb)
            {
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroAccordionActivated", ApplyAccordionGloballyCB.Checked.ToString());
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroFloatedRibbon", FloatRibbonCB.Checked.ToString());
                Utilities.SPPropertyBagHelper.AddPropertyAndUpdate(SPContext.Current.Site.RootWeb, "GlobalMetroQuickLaunchPosition", QuickLaunchPosition.SelectedValue);
            }

            Response.Redirect(Request.RawUrl);
        }

        /// <summary>
        /// Gets the Values from the Property Bag to be used within the Admin Page
        /// </summary>
        private void GetPropertyBagValues()
        {
            Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "InheritMetroThemeColour"));

            Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "MetroThemeColour");


            //Accordion
            StoredAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "MetroAccordionActivated"));

            StoredGlobalAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroAccordionActivated"));

            StoredGlobalRibbonValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroFloatedRibbon"));

            StoredQuicklaunchPositionValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Site.RootWeb, "GlobalMetroQuickLaunchPosition");

            StoredAccordionTypeValue = Utilities.SPPropertyBagHelper.GetPropertyValue(SPContext.Current.Web, "GlobalMetroAccordionType");
        }

        /// <summary>
        /// Enables and Disables Controls on the page based on location and values set in GetPropertyBagValues()
        /// </summary>
        private void DisableControlChecker()
        {

            //If we are on the root site
            if (SPContext.Current.Web.IsRootWeb)
            {
                AccordionStatusMessage.Visible = false;
            }
            //If we are NOT on the root site
            else
            {
                QuickLaunchPosition.Visible = false;
                
                if (StoredGlobalAccordionValue)
                {
                    AccordionCheckbox.Visible = false;
                    ApplyAccordionGloballyCB.Visible = false;
                    AccordionTypeDropdown.Visible = false;
                    FloatRibbonCB.Visible = false;
                }
            }
        }


        private void PopulateQuickLaunchPositionDropDown(DropDownList dropDownList, string themeQuicklaunchPosition)
        {
            string[] positions = new string[] { "Right", "Left" };
            foreach (string position in positions)
            {
                dropDownList.Items.Add(position);
            }

            QuickLaunchPositionLabel.Text = string.IsNullOrEmpty(themeQuicklaunchPosition) ? positions[0] : themeQuicklaunchPosition;

            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == themeQuicklaunchPosition)
                {
                    dropDownList.SelectedIndex = i;
                }
            }
        }

        private void PopulateAccordionTypeDropDown(DropDownList dropDownList, string storedAccordionValue)
        {
            string[] types = new string[] { "Hover", "Click" };
            foreach (string type in types)
            {
                dropDownList.Items.Add(type);
            }

            QuickLaunchPositionLabel.Text = string.IsNullOrEmpty(storedAccordionValue) ? types[0] : storedAccordionValue;

            for (int i = 0; i < dropDownList.Items.Count; i++)
            {
                if (dropDownList.Items[i].Value == storedAccordionValue)
                {
                    dropDownList.SelectedIndex = i;
                }
            }
        }
    }
}
